package com.usian.controller;

import com.alibaba.fastjson.JSONObject;
import com.usian.feign.ProviderFeign;
import com.usian.model.dtos.XiansuoDto;
import com.usian.model.pojos.Provinces;
import com.usian.model.pojos.Xiansuo;
import com.usian.model.pojos.XiansuoResult;
import com.usian.utils.AppHttpCodeEnum;
import com.usian.utils.ResponseResult;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/xiansuo")
public class XiansuoController {

    @Autowired
    private ProviderFeign providerFeign;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 线索列表分页查询 feign远程调用
     *
     * @param dto
     * @return
     */
    @PostMapping("/list")
    public ResponseResult list(@RequestBody XiansuoDto dto){
        return providerFeign.list(dto);
    }

    /**
     * 获取省市区三级联动 feign远程调用
     *
     * @param parentId
     * @param rank
     * @return
     */
    @GetMapping("/pro")
    public List<Provinces> proList(@RequestParam Integer parentId, @RequestParam Integer rank){
        return providerFeign.proList(parentId,rank);
    }

    /**
     * 保存线索信息 feign远程调用
     *
     * @param xiansuo
     * @return
     */
    @PostMapping("/save")
    public ResponseResult save(@RequestBody Xiansuo xiansuo){
        return providerFeign.save(xiansuo);
    }

    /**
     * 删除线索信息 feign远程调用
     * @param id
     * @return
     */
    @GetMapping("/deleteById")
    public ResponseResult deleteById(@RequestParam Integer id){
        return providerFeign.deleteById(id);
    }

    /**
     * 线索审核 feign远程调用
     *
     * @param xiansuo
     * @return
     */
    @PostMapping("/shenhe")
    public ResponseResult shenhe(@RequestBody Xiansuo xiansuo){
        return providerFeign.shenhe(xiansuo);
    }

    /**
     * 线索办结 feign远程调用
     *
     * @param xiansuoResult
     * @return
     */
    @PostMapping("/banjie")
    public ResponseResult banjie(@RequestBody XiansuoResult xiansuoResult){

        //封装办结数据
        xiansuoResult.setUserName("张三");
        String statusStr =xiansuoResult.getStatus()==0?"未办结":"已办结";
        xiansuoResult.setContent(xiansuoResult.getUserName()+"您好，您提交的"+xiansuoResult.getXname()+statusStr);

        //mq发送办结通知消息
        rabbitTemplate.convertAndSend("loopMessage", JSONObject.toJSONString(xiansuoResult));

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
